<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>

</style>

<body>

  <div class="box"></div>
  <script>
    // 方法一：

    function guid () {
      return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0,
          v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
      });
    }
    guid() // "a1ca0f7b-51bd-4bf3-a5d5-6a74f6adc1c7"

    // 方法二：

    var _S4 = function () {
      return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
    }

    // 获取广告请求唯一ID 方式是当前时间戳+13位随机吗
    export function GetsingleId () {
      var _res = (_S4() + _S4() + '-' + _S4() + '-' + _S4() + '-' + _S4() + '-' + _S4() + _S4() + _S4())
      return '_' + config.version + '_' + _res
    }

    // 方法三：

    function uuid () {
      var s = [];
      var hexDigits = "0123456789abcdef";
      for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
      }
      s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
      s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
      s[8] = s[13] = s[18] = s[23] = "-";

      var uuid = s.join("");
      return uuid;
    }
    uuid() // "ffb7cefd-02cb-4853-8238-c0292cf988d5"

    // NanoID的方式
    import { nanoid } from 'nanoid'
    let idA = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
    //也可以指定生成字符串的长度
    let idB = nanoid(5)
    // 主要可以通过npm包的形式安装，核心代码如下：

    let urlAlphabet =
      'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'

    let nanoid = (size = 21) => {
      let id = ''
      // A compact alternative for `for (var i = 0; i < step; i++)`.
      let i = size
      while (i--) {
        // `| 0` is more compact and faster than `Math.floor()`.
        id += urlAlphabet[(Math.random() * 64) | 0]
      }
      return id
    }


  </script>
</body>

</html>